package com.moneyanalyzer.adapter;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.moneyanalyzer.R;
import com.moneyanalyzer.model.Transaction;

public class TransactionAdapter extends BaseAdapter {
	private Context context;
	private List<Transaction> transactions;
	
	public TransactionAdapter(Context context, List<Transaction> transactions){
		this.context = context;
		this.transactions = transactions;
	}

	@Override
	public int getCount() {
		return transactions.size();
	}

	@Override
	public Object getItem(int position) {
		return transactions.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Transaction transaction = transactions.get(position);
		LayoutInflater inflater = (LayoutInflater)
	            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.item_list_transactions, null);
		TextView transactionDate = (TextView)view.findViewById(R.id.transactionDate);
		TextView transactionDescription = (TextView)view.findViewById(R.id.transactionDescription);
		TextView transactionValue = (TextView)view.findViewById(R.id.transactionValue);		
		transactionDate.setText(new SimpleDateFormat("dd/MM/yy HH:mm").format(new Date(transaction.getDate())));
		transactionDescription.setText(transaction.getDescription());
		
		
		transactionValue.setText(String.format("R$%.02f", transaction.getValue()));
		return view;
	}

}
